//
//  ShopOrdersViewController.swift
//  iModern
//
//  Created by Ralf on 8/11/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class ShopOrdersViewController: TableViewController
{

    var segmentedControl: UISegmentedControl?

    var articles    = NSMutableArray()
    var appointment = NSMutableArray()
    var max:    Int = 10
    var offset: Int = 0
    var end         = false
    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    var quanData = NSMutableDictionary()

    var appointmentManager = AppointmentManager()
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.initBackButton()
        self.navigationItem.title = "订单管理"
        self.tableView.registerNib(UINib(nibName: "ShopOrdersViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "ShopOrdersView")

        if segmentedControl == nil
        {
            let view1 = UIView()
            view1.frame = CGRectMake(0, 10, 180, 50)
            view1.backgroundColor = UIColor.groupTableViewBackgroundColor()
            self.tableView!.tableHeaderView = view1
            
            let screen = UIScreen.mainScreen().bounds
            segmentedControl = UISegmentedControl(items: ["未评价", "已评价"])
            segmentedControl!.tintColor = Theme.theme_color
            segmentedControl?.frame = CGRectMake((screen.size.width - 180) / 2, 10, 180, 28)
            segmentedControl!.selectedSegmentIndex = 0
            segmentedControl!.addTarget(self, action: "segmentSelected:", forControlEvents: .ValueChanged)
            view1.addSubview(segmentedControl!)
            
//            segmentedControl = UISegmentedControl(items: ["未评价", "已评价"])
//            segmentedControl!.tintColor = Theme.theme_color
//            segmentedControl?.frame = CGRectMake((screen.size.width - 180) / 2, 10, 180, 28)
//            segmentedControl!.selectedSegmentIndex = 0
//            segmentedControl!.addTarget(self, action: "segmentSelected:", forControlEvents: .ValueChanged)
//            view1.addSubview(segmentedControl!)
        }

        loadData()
    }

    func loadData()
    {
        if (end)
        {
            return;
        }

        if (appDelegate.isLogged())
        {

            var rs: ResultSet?

            if self.segmentedControl!.selectedSegmentIndex == 0
            {
                rs = appointmentManager.queryAppointmentByType("toMe", max: max, offset: offset, reviewed: false)
            }
            else
            {
                rs = appointmentManager.queryAppointmentByType("toMe", max: max, offset: offset, reviewed: true)
            }

            if (self.verifyResutSet(rs))
            {
                if (rs!.isEmpty())
                {
                    end = true
                }
                else
                {
                    articles.addObjectsFromArray(rs!.toMutableArray() as [AnyObject])

                    offset = articles.count
                }
            }
        }

        self.tableView.reloadData()
    }

    func segmentSelected(sender: UISegmentedControl)
    {
        reloadData(sender)
    }

    @IBAction override func reloadData(sender: AnyObject?)
    {
        offset = 0
        end = false
        articles.removeAllObjects()
        self.loadData()
        super.reloadData(sender)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()

    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {

        return articles.count
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return 180
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell = tableView.dequeueReusableCellWithIdentifier("ShopOrdersView", forIndexPath: indexPath) as! ShopOrdersViewCell

        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        quanData = articles[indexPath.row] as! NSMutableDictionary
        cell.setData(quanData)
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
              
        let controller = ShowOrdersFormViewController()
        let article=articles[indexPath.row] as! NSMutableDictionary

        controller.order = article
        self.pushViewController(controller)
        
    }

}
